(function($) {
  /*扩展插件：
   * 1.$.time->时间插件
   *  -- 时间戳获取时间：$.time.getTime()
   *  -- 普通时间获取时间戳:  $.time.dataCommon("2012-11-12 02:02:03")
   *  -- 判断是否今天   $.time.isToday(时间戳)
   * 	-- 当前星期 几  $.time.weekTime(时间戳) 
   * 	-- str时间段的一个星期时间和日期$.time.LoadDayTime(时间戳) 
   *  -- 加减时间天数   $.time.addTime(a,b) 
   * 2.-----未完待续
  */
  $.extend({
    time: {
      getZf: function(num) {
        if (parseInt(num) < 10) {
          num = "0" + num;
        }
        return num;
      },
      getTime: function(str, type, n) {
        /*
         * 函数名：   $.time.getTime(101444441,'YMD','-') 
         * 函数功能： 时间戳获取时间
         * 传入参数： str:时间戳
         *         y:传入返回类型     YMD/ymr:年月日  ； YMDHMS/ymdhms:年月日时分秒 HMS/hms:时分秒
         *         n:年月日分隔符  ---默认为"-"
         * 返回值：  默认：Y + M + D + h + m + s
         *
        */
        var date = new Date(str);
        var dateTime = "";
        n = n != null ? n : "-";
        Y = date.getFullYear() + n;
        M =
          (date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) + n;
        D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
        h = date.getHours() + ":";
        m = date.getMinutes() + ":";
        s = date.getSeconds();
        if (type == "ym" || type == "YM") {
          dateTime = Y + M;
        } else if (type == "ymd" || type == "YMD") {
          dateTime = Y + M + D;
        } else if (type == "hms" || type == "HMS") {
          dateTime = h + m + s;
        } else {
          dateTime = Y + $.time.getZf(M) + D+ $.time.getZf(h) + $.time.getZf(m) + $.time.getZf(s);
        }
        return dateTime;
      },

      dataCommon: function(date) {
        /*
         * 函数名：$.time.dataCommon("2012-11-12 02:02:03") 
         * 函数功能：普通时间获取时间戳
         * 传入参数： date：2012-11-12 02:02:03， 2012/11/12 02:02:03 ，2012.11.12 02:02:03
         * 返回值：时间戳
         */
        date = new Date(Date.parse(date.replace(/-/g, "/")));
        date = date.getTime();
        return date;
      },
      isToday: function(str) {
        /*
       * 函数名：$.time.isToday(14895465465) 
       * 函数功能：判断是否今天
       * 传入参数： str：时间戳
       * 返回值：true：是今天  false: 不是今天
       */
        if (new Date(str).toDateString() === new Date().toDateString()) {
          return true;
        } else if (new Date(str) < new Date()) {
          return false;
        }
      },
      weekTime: function(date) {
        /*
        * 函数名：$.time.weekTime(14895465465) 
        * 函数功能：当前这个星期
        * 传入参数： date:时间戳
        * 返回值：week：星期
        */
        var week;
        if (date.getDay() == 0) week = "星期日";
        if (date.getDay() == 1) week = "星期一";
        if (date.getDay() == 2) week = "星期二";
        if (date.getDay() == 3) week = "星期三";
        if (date.getDay() == 4) week = "星期四";
        if (date.getDay() == 5) week = "星期五";
        if (date.getDay() == 6) week = "星期六";
        return week;
      },
      LoadDayTime: function(str) {
        /*
        * 函数名：$.time.LoadDayTime() 
        * 函数功能：获取str这个时间段的这个星期
        * 传入参数： str：时间戳
        * 返回值：weekArray 数组:
        */
        var now = new Date(str); //当前时间
        var nowTime = now.getTime();
        var day = now.getDay(); //当前星期几
        var oneDayLong = 24 * 60 * 60 * 1000;
        var sevenTime = $.time.getTime(nowTime - day * oneDayLong, "YMD", "-"); //0
        var oneTime = $.time.getTime(
          nowTime - (day - 1) * oneDayLong,
          "YMD",
          "-"
        ); //1
        var twoTime = $.time.getTime(
          nowTime - (day - 2) * oneDayLong,
          "YMD",
          "-"
        ); //2
        var threeTime = $.time.getTime(
          nowTime - (day - 3) * oneDayLong,
          "YMD",
          "-"
        ); //3
        var fourTime = $.time.getTime(
          nowTime - (day - 4) * oneDayLong,
          "YMD",
          "-"
        ); //4
        var fiveTime = $.time.getTime(
          nowTime - (day - 5) * oneDayLong,
          "YMD",
          "-"
        ); //5
        var sixTime = $.time.getTime(
          nowTime - (day - 6) * oneDayLong,
          "YMD",
          "-"
        ); //6
        sevenTime = [$.time.weekTime(new Date(sevenTime)), sevenTime];
        oneTime = [$.time.weekTime(new Date(oneTime)), oneTime];
        twoTime = [$.time.weekTime(new Date(twoTime)), twoTime];
        threeTime = [$.time.weekTime(new Date(threeTime)), threeTime];
        fourTime = [$.time.weekTime(new Date(fourTime)), fourTime];
        fiveTime = [$.time.weekTime(new Date(fiveTime)), fiveTime];
        sixTime = [$.time.weekTime(new Date(sixTime)), sixTime];
        var weekArray = [
          {
            sevenTime: sevenTime,
            oneTime: oneTime,
            twoTime: twoTime,
            threeTime: threeTime,
            fourTime: fourTime,
            fiveTime: fiveTime,
            sixTime: sixTime
          }
        ];
        return weekArray;
      },
      addTime: function(day, str) {
        /*
          * 函数名：$.time.addTime() 
          * 函数功能：加减天数
          * 传入参数： day： 5/-5  str:指定时间戳
          * 返回值：返回年月日
          */
        var nowTime = str == null ? new Date().getTime() : str;
        var dayTime = day * 24 * 60 * 60 * 1000; //参数天数的时间戳
        var myDate = new Date(nowTime + dayTime); //把两个时间戳转换成普通时间
        var y = myDate.getFullYear();
        var m = myDate.getMonth() + 1;
        var d = myDate.getDate();
        return y + "-" + m + "-" + d;
      }
    }
  });
})(jQuery);
